package frontend_gui;

import java.awt.EventQueue;

import java_persistence.PatientMapper;
import java_persistence.UserMapper;

import javax.swing.JFrame;

import org.apache.ibatis.session.SqlSession;

import domain.Patient;
import domain.User;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class NewUserRegistration {

	private JFrame frmPatientRegistration;
	private JTextField textField; //phone
	private JTextField textField_1; //address
	private JTextField textField_2; //email
	private JTextField textField_3; //pharmacy
	private JTextField textField_4; // insurance
	private JTextField textField_5; //allergies
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField textField_6; // name
		
	private static Patient my_new_user;
	private static SqlSession my_session;
	
	/**
	 * Launch the application.
	 */
	public static void main(final User new_user, final SqlSession session) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					my_new_user=(Patient) new_user;
					my_session = session;
					NewUserRegistration window = new NewUserRegistration();
					window.frmPatientRegistration.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	private NewUserRegistration() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmPatientRegistration = new JFrame();
		frmPatientRegistration.setTitle("Patient Registration");
		frmPatientRegistration.setBounds(100, 100, 545, 400);
		frmPatientRegistration.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmPatientRegistration.getContentPane().setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(255, 43, 179, 28);
		frmPatientRegistration.getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblPhoneNumber = new JLabel("Phone Number:");
		lblPhoneNumber.setBounds(141, 49, 102, 16);
		frmPatientRegistration.getContentPane().add(lblPhoneNumber);
		
		JLabel lblNewLabel = new JLabel("Address:");
		lblNewLabel.setBounds(141, 86, 61, 22);
		frmPatientRegistration.getContentPane().add(lblNewLabel);
		
		textField_1 = new JTextField();
		textField_1.setBounds(255, 83, 179, 28);
		frmPatientRegistration.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(255, 123, 179, 28);
		frmPatientRegistration.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(141, 129, 51, 16);
		frmPatientRegistration.getContentPane().add(lblEmail);
		
		JButton btnNewButton = new JButton("Register New Patient");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				my_new_user.setActive_account(true);
				my_new_user.setAddress(textField_1.getText());
				my_new_user.setEmail(textField_2.getText());
				my_new_user.setName(textField_6.getText());
				my_new_user.setPhone_number(textField.getText());
				my_new_user.setBirthdate(null); // <<-----fix this one
				my_new_user.setInsurance_info(textField_4.getText());
				my_new_user.setPharmacy(textField_3.getText());
				my_new_user.setAllergies(textField_5.getText());
				// Something about button groups?
				my_new_user.setIs_male(null);
				
				((User)my_new_user).create(my_session.getMapper(UserMapper.class));
				my_new_user.create_patient(my_session.getMapper(PatientMapper.class));
				
				my_session.commit();
			}
		});
		btnNewButton.setBounds(188, 329, 133, 23);
		frmPatientRegistration.getContentPane().add(btnNewButton);
		
		textField_3 = new JTextField();
		textField_3.setBounds(255, 163, 179, 28);
		frmPatientRegistration.getContentPane().add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblPharmacy = new JLabel("Pharmacy:");
		lblPharmacy.setBounds(141, 169, 73, 16);
		frmPatientRegistration.getContentPane().add(lblPharmacy);
		
		textField_4 = new JTextField();
		textField_4.setBounds(255, 203, 179, 28);
		frmPatientRegistration.getContentPane().add(textField_4);
		textField_4.setColumns(10);
		
		JLabel lblInsuranceInfo = new JLabel("Insurance Info:");
		lblInsuranceInfo.setBounds(141, 209, 102, 16);
		frmPatientRegistration.getContentPane().add(lblInsuranceInfo);
		
		textField_5 = new JTextField();
		textField_5.setBounds(255, 243, 179, 28);
		frmPatientRegistration.getContentPane().add(textField_5);
		textField_5.setColumns(10);
		
		JLabel lblAllergies = new JLabel("Allergies:");
		lblAllergies.setBounds(141, 249, 61, 16);
		frmPatientRegistration.getContentPane().add(lblAllergies);
		
		JRadioButton rdbtnMale = new JRadioButton("Male");
		buttonGroup.add(rdbtnMale);
		rdbtnMale.setBounds(141, 288, 61, 23);
		frmPatientRegistration.getContentPane().add(rdbtnMale);
		
		JRadioButton rdbtnFemale = new JRadioButton("Female");
		buttonGroup.add(rdbtnFemale);
		rdbtnFemale.setBounds(204, 288, 141, 23);
		frmPatientRegistration.getContentPane().add(rdbtnFemale);
		
		textField_6 = new JTextField();
		textField_6.setBounds(255, 6, 179, 28);
		frmPatientRegistration.getContentPane().add(textField_6);
		textField_6.setColumns(10);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(141, 12, 61, 16);
		frmPatientRegistration.getContentPane().add(lblName);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(345, 329, 89, 23);
		frmPatientRegistration.getContentPane().add(btnCancel);
	}
}
